﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace 瓦片截图
{
    public partial class MainForm : Form
    {
        private OverlayForm overlay;

        // 用于模拟鼠标操作的API
        [DllImport("user32.dll")]
        private static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, int dwExtraInfo);

        private const int MOUSEEVENTF_MOVE = 0x01; //移动鼠标 
        private const int MOUSEEVENTF_LEFTDOWN = 0x02; //模拟鼠标左键按下
        private const int MOUSEEVENTF_LEFTUP = 0x04; //模拟鼠标左键抬起 

        private int pngX = 0;
        private int pngY = 0;

        public MainForm()
        {
            InitializeComponent();
            InitializeCustomComponents();

            // 创建透明覆盖窗口
            overlay = new OverlayForm();
            overlay.Show();
        }

        private void InitializeCustomComponents()
        {
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.Text = "瓦片截图X=" + pngX.ToString() + ",Y=" + pngY.ToString();
            // 确保截图目录存在
            string captureDir = Path.Combine(Application.StartupPath, "ScreenCapture");
            if (!Directory.Exists(captureDir))
            {
                Directory.CreateDirectory(captureDir);
            }
        }

        private void SimulateDrag(Point start, Point end, int steps, int delay)
        {
            // 备份原始鼠标位置
            Point originalPosition = Cursor.Position;

            // 隐藏覆盖窗口
            overlay.Hide();
            Application.DoEvents();
            Thread.Sleep(100); // 等待窗口隐藏

            try
            {
                // 移动鼠标到起点
                Cursor.Position = start;
                Thread.Sleep(50);

                // 按下鼠标左键
                mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
                Thread.Sleep(50);

                // 分步移动鼠标
                int dx = (end.X - start.X) / steps;
                int dy = (end.Y - start.Y) / steps;

                for (int i = 0; i < steps; i++)
                {
                    Cursor.Position = new Point(
                        Cursor.Position.X + dx,
                        Cursor.Position.Y + dy);
                    Thread.Sleep(delay);
                }

                // 确保到达终点
                Cursor.Position = end;
                Thread.Sleep(50);

                // 释放鼠标左键
                mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
            }
            finally
            {
                // 恢复鼠标到原始位置
                Cursor.Position = originalPosition;

                // 恢复覆盖窗口
                overlay.Show();
            }
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            pngY = pngY - 1;
            this.Text = "瓦片截图X=" + pngX.ToString() + ",Y=" + pngY.ToString();
            // 从矩形框左上角拖动到左下角
            Point start = new Point(
                overlay.SelectionRect.Left + overlay.SelectionRect.Width/2,
                overlay.SelectionRect.Top);
            Point end = new Point(
                overlay.SelectionRect.Left + overlay.SelectionRect.Width/2,
                overlay.SelectionRect.Bottom);

            SimulateDrag(start, end,10,50);
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            pngY = pngY + 1;
            this.Text = "瓦片截图X=" + pngX.ToString() + ",Y=" + pngY.ToString();
            // 从矩形框左下角拖动到左上角
            Point start = new Point(
                overlay.SelectionRect.Left + overlay.SelectionRect.Width/2,
                overlay.SelectionRect.Bottom);
            Point end = new Point(
                overlay.SelectionRect.Left + overlay.SelectionRect.Width/2,
                overlay.SelectionRect.Top);

            SimulateDrag(start, end, 10, 50);
        }

        private void btnLeft_Click(object sender, EventArgs e)
        {
            pngX = pngX - 1;
            this.Text = "瓦片截图X=" + pngX.ToString() + ",Y=" + pngY.ToString();
            // 从矩形框左上角拖动到右上角
            Point start = new Point(
                overlay.SelectionRect.Left,
                overlay.SelectionRect.Top + overlay.SelectionRect.Height/2);
            Point end = new Point(
                overlay.SelectionRect.Right,
                overlay.SelectionRect.Top + overlay.SelectionRect.Height/2);

            SimulateDrag(start, end, 10, 50);
        }

        private void btnRight_Click(object sender, EventArgs e)
        {
            pngX = pngX + 1;
            this.Text = "瓦片截图X=" + pngX.ToString() + ",Y=" + pngY.ToString();
            // 从矩形框右上角拖动到左上角
            Point start = new Point(
                overlay.SelectionRect.Right,
                overlay.SelectionRect.Top + overlay.SelectionRect.Height/2);
            Point end = new Point(
                overlay.SelectionRect.Left,
                overlay.SelectionRect.Top + overlay.SelectionRect.Height/2);

            SimulateDrag(start, end, 10, 50);
        }

        private void btnCapture_Click(object sender, EventArgs e)
        {
            // 隐藏覆盖窗口
            overlay.Hide();
            Application.DoEvents();
            Thread.Sleep(100); // 等待窗口隐藏

            try
            {
                CaptureScreen();
            }
            finally
            {
                // 恢复覆盖窗口
                overlay.Show();
            }
        }

        private void CaptureScreen()
        {
            try
            {
                using (Bitmap bmp = new Bitmap(overlay.SelectionRect.Width, overlay.SelectionRect.Height))
                {
                    textBox1.Text = "";
                    using (Graphics g = Graphics.FromImage(bmp))
                    {
                        g.CopyFromScreen(overlay.SelectionRect.Location, Point.Empty, overlay.SelectionRect.Size);
                    }

                    string fileName = "瓦片X" + pngX.ToString() + "_Y" + pngY.ToString() + ".png";
                    string dirPath = Path.Combine(Application.StartupPath, "ScreenCapture");
                    string filePath = Path.Combine(dirPath, fileName);

                    bmp.Save(filePath, ImageFormat.Png);
                    textBox1.Text = "截图已保存到" + filePath;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("截图失败: {0}", ex.Message), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            overlay.Close();
        }
    }
}